package com.gxuwz.orderprovider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.Order;
import com.gxuwz.orderprovider.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 通过id查询订单信息
     * @param orderID
     * @return
     */
    @GetMapping("/findById/{orderID}")
    public Order getOrder(@PathVariable("orderID") Integer orderID){
        return orderMapper.findByID(orderID);
    }

    /**
     * 查询所有订单信息
     * @param order
     * @return
     */
    @GetMapping("/list")
    public List<Order> list(@RequestBody Order order){
        return orderMapper.query(order);
    }

    /**
     * 添加订单信息
     * @param order
     */
    @PostMapping("/add")
    public void addOrder(@RequestBody Order order){
        orderMapper.insert(order);
    }

    /**
     * 通过orderID删除订单信息
     * @param orderID
     */
    @DeleteMapping("/delete/{orderID}")
    public void deleteOrder(@PathVariable("orderID") Integer orderID){
        orderMapper.delete(orderID);
    }

    /**
     * 更新订单信息
     * @param order
     */
    @PutMapping("/update")
    public void editOrder(@RequestBody Order order){
        orderMapper.update(order);
    }

    /**
     * 分页（不带条件）
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public PageInfo findPage(@RequestParam(value = "pageIndex") int pageIndex, @RequestParam(value = "pageSize") int pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<Order> orders = orderMapper.findPage(null);
        PageInfo<Order> page = new PageInfo<>(orders);
        return page;
    }
}
